package com.cloud.tsp.controller;

import com.cloud.api.domain.model.VehicleModel;
import com.cloud.api.domain.tsp.*;
import com.cloud.core.util.R;
import com.cloud.api.domain.forward.QueryVehicleSale;
import com.cloud.api.domain.forward.ResponseData;
import com.cloud.api.domain.stats.VehicleStats;
import com.cloud.api.constants.StrVo;
import com.cloud.log.annotation.Log;
import com.cloud.log.enums.BusinessType;
import com.cloud.log.enums.OperatorType;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.service.IVehicleMessageDailyService;
import com.cloud.tsp.service.IVehicleModelService;
import com.cloud.tsp.service.IVehicleSaleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author haohaibo
 * @since 2023-12-20
 */
@RestController
@Validated
@RequestMapping("/vehicle-sale")
public class VehicleSaleController {

    @Resource
    private IVehicleSaleService vehicleSaleService;
    @Resource
    private IVehicleModelService vehicleModelService;
    @Resource(name = "myRedis")
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private StreamBridge streamBridge;

    @Tag(name = "v1.0")
    @Operation(summary = "销售数据（管理）")
    @PostMapping("/getVehicleSaleList")
    @Log(title = "销售车辆管理-销售数据", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public TableDataInfo<VehicleSale> selectVehicleSaleList(@RequestBody @Valid QueryVehicleProduction para){
        return vehicleSaleService.selectVehicleSaleList(para);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "销售数据详情（管理）")
    @PostMapping("/getById")
    @Log(title = "销售车辆管理-销售数据详情", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<VehicleSale> getById(@RequestBody @Valid StrVo para){
        VehicleSale byId = vehicleSaleService.getById(para);
        VehicleModel byId1 = vehicleModelService.getById(byId.getModelType());
        byId.setModelName(byId1.getModelName());
        return R.ok(byId);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "查询组织机构下的销售车辆数（管理）")
    @PostMapping("/getVehicleCountByDept")
    @Log(title = "销售车辆管理-查询组织机构下的销售车辆数", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<Long> selectVehicleCountByDept(@RequestBody List<String> deptId){
        return R.ok(vehicleSaleService.selectVehicleCountByDept(deptId));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "首页统计接入车辆数（tsp管理）")
    @PostMapping("/getVehicleCount")
    @Log(title = "首页-首页统计接入车辆数", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<VehicleStats> selectVehicleCount(){
        return R.ok(vehicleSaleService.selectVehicleCount());
    }

    @Tag(name = "v1.0")
    @Operation(summary = "销售数据（用于转发平台的车辆查询）")
    @PostMapping("/getVehicleForwardList")
    @Log(title = "销售车辆管理-销售数据（用于转发平台的车辆查询）", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public TableDataInfo<VehicleSale> selectVehicleForwardList(@RequestBody @Valid QueryVehicleSale para){
        return vehicleSaleService.selectVehicleForwardList(para);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "查询实时位置的车辆基本信息（管理）")
    @PostMapping("/getVehicleBaseInformation")
    @Log(title = "实时监控-查询实时位置的车辆基本信息", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<VehicleBaseVo> selectVehicleBaseInformation(@RequestBody @Valid StrVo para){
        return R.ok(vehicleSaleService.selectVehicleBaseInformation(para.getId()));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "联想vin（ tsp 车辆实时位置管理）")
    @PostMapping("/getVinScopeTsp")
    //TODO 是否修改
    @Log(title = "销售车辆管理-联想vin", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<VinLockStatus> selectVinScopeTsp(@RequestBody @Valid StrVo para){
        return R.ok(vehicleSaleService.selectVinScopeTsp(para.getId()));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "销售数据新增修改（管理）")
    @PostMapping("/insertOrUpdate")
    @Log(title = "销售车辆管理-销售数据新增修改", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R insertOrUpdate(@RequestBody @Valid VehicleSale para){
        vehicleSaleService.insertOrUpdate(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "销售数据删除（管理）")
    @PostMapping("/deleteById")
    @Log(title = "销售车辆管理-销售数据删除", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    public R deleteById(@RequestBody @Valid StrVo para){
        VehicleSale vehicleSale = vehicleSaleService.getById(para.getId());
        vehicleSaleService.removeById(para.getId());
        streamBridge.send("VEH_CHANGE-out-0", MessageBuilder.withPayload("VEH_CHG").build());
//        redisTemplate.opsForHash().delete("VIN-INFORMATION",vehicleSale.getVin());
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "销售数据批量修改组织机构（管理）")
    @PostMapping("/updateByIds")
    @Log(title = "销售车辆管理-销售数据批量修改组织机构", businessType = BusinessType.UPDATE, operatorType = OperatorType.MANAGE)
    public R updateByIds(@RequestBody @Valid BatchId para){
        vehicleSaleService.updateByIds(para);
        return R.ok();
    }

    @Tag(name = "v1.0")
    @Operation(summary = "查询vin是否存在")
    @PostMapping("/checkExist")
    @Log(title = "销售车辆管理-查询vin是否存在", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R checkExist(@RequestBody @Valid StrVo para) throws IOException {
        return R.ok(vehicleSaleService.checkExist(para.getId()));
    }

    @Tag(name = "v1.0")
    @Operation(summary = "下载模板")
    @PostMapping("/downloadTemplate")
    @Log(title = "销售车辆管理-下载模板", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        vehicleSaleService.downloadTemplate(response);
    }

    /*@Tag(name = "v1.0")
    @Operation(summary = "导出销售数据")
    @PostMapping("/exportSaleVinList")
    @Log(title = "销售车辆管理-导出销售数据", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
    public void exportSaleVinList(QueryVehicleProduction para, HttpServletResponse response) throws IOException {
        vehicleSaleService.exportSaleVinList(para,response);
    }*/

    @Tag(name = "v1.0")
    @Operation(summary = "导入销售数据表")
    @PostMapping("/importSaleVinList")
    @Log(title = "销售车辆管理-导入销售数据表", businessType = BusinessType.IMPORT, operatorType = OperatorType.MANAGE)
    public R<ResponseData> importSaleVinList(@RequestParam MultipartFile file, @NotEmpty String deptId, HttpServletResponse response) throws IOException {
        String key = "sale";
        ResponseData responseData = vehicleSaleService.importSaleVinList(deptId, file, response, key);
        return R.ok(responseData);
    }

    @Tag(name = "v1.0")
    @Operation(summary = "根据id查询vin")
    @PostMapping("/getVinById")
    @Log(title = "销售车辆管理-根据id查询vin", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE)
    public R<String> getVinById(@RequestBody @Valid StrVo para) throws IOException {
        return R.ok(vehicleSaleService.getById(para.getId()).getVin());
    }
}